package com.minisoft.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.*;

/**
 * @author JueSu
 * @description: 移动案场刷数
 * @date 2021/10/13 9:36
 */
public class FreshData {

    public static void main(String[] args) {
        FreshData f = new FreshData();
        f.genSalerAuth();
    }

    public void genSalerAuth() {
        Collection<String> list = new ArrayList<>();
        genSalerAuth(list);
        System.out.println("需要补数据量：" + list.size());
        String path = "D:/temp/salerAuth1013.txt";
        FileUtil.writeLines(list,path,"utf-8");
    }

    private void genSalerAuth(Collection<String> list) {
        Map<String,Integer> roleMap = new HashMap<>();
        roleMap.put("置业顾问",2011);
        roleMap.put("销售主管",2012);
        roleMap.put("销售经理",2015);

        String insertSalerAuth = "INSERT INTO `dh_project_saler_auth` (`appid`, `saleropenid`, `projectid`, `createtime`, `roleid`) VALUES ('{}', '{}', {}, '{}', {});";

        JSONArray centerArray = JSONUtil.parseObj(FileUtil.readUtf8String("D:/temp/客户中心测试.json")).getJSONArray("RECORDS");
        JSONArray crmArray = JSONUtil.parseObj(FileUtil.readUtf8String("D:/temp/移动案场查询.json")).getJSONArray("RECORDS");
        Map<String,Boolean> ydacMap = new HashMap<>();
        String now = DateUtil.now();

        //sale_org_id->id
        Map<String,Integer> rsProjectMap = new HashMap<>();
        //userkey->{appid,saleropenid}
        Map<String,JSONObject> salerMap = new HashMap<>();

        List<String> errorList1 = new ArrayList<>();
        List<String> errorList2 = new ArrayList<>();

        for (Object o : crmArray) {
            JSONObject obj = (JSONObject) o;
            rsProjectMap.put(obj.getStr("sale_org_id"),obj.getInt("pid"));
            salerMap.put(obj.getStr("userkey"),obj);
            ydacMap.put(obj.getStr("skey"),true);
        }
        for (Object o : centerArray) {
            JSONObject obj = (JSONObject) o;
            if (!ydacMap.containsKey(obj.getStr("skey"))) {
                //不存在，需要补数据(`appid`, `saleropenid`, `projectid`, `createtime`
                JSONObject saler = salerMap.get(obj.getStr("user_key"));
                if (saler == null) {
                    errorList1.add(obj.getStr("user_key") + "在移动案场未注册");
                    continue;
                }
                Integer pid = rsProjectMap.get(obj.getStr("sale_org_id"));
                if (pid == null) {
                    errorList2.add(obj.getStr("sale_org_id") + "销售组织在移动案场未注册");
                    continue;
                }

                String sql = StrUtil.format(insertSalerAuth, saler.getStr("appid"), saler.getStr("saleropenid"),
                        pid, now);
                list.add(sql);
            }
        }

        for (int i = 0; i < errorList1.size(); i++) {
            System.out.println((i+1) + "." + errorList1.get(i));
        }
        for (int i = 0; i < errorList2.size(); i++) {
            System.out.println((i+1) + "." + errorList2.get(i));
        }

    }
}

